package com.listener.controller;

import com.listener.dao.AbilityDao;
import com.listener.dao.MoveDao;
import com.listener.dao.PokemonDao;
import com.listener.dao.TypeDao;
import com.listener.pojo.Page;
import com.listener.pojo.Pokemon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@Controller
public class PokemonController {
    @Autowired
    PokemonDao pokemonDao;
    @Autowired
    TypeDao typeDao;
    @RequestMapping("/selectPokemonPage")
    public String selectPokemonPage(Pokemon pokemonCondition,Integer pageNum, HttpServletRequest request) throws IOException {
        if(pageNum==null){pageNum=1;}
        //如果属性2有 属性1空 交换
        if((pokemonCondition.getType1()!=null && !pokemonCondition.getType1().equals(""))
                &&
                (pokemonCondition.getType()==null || pokemonCondition.getType().equals(""))){
            pokemonCondition.setType(pokemonCondition.getType1());
            pokemonCondition.setType1(null);
        }
        //如果条件不为空且不为""记录条件到session
        if((pokemonCondition.getType()!=null ||
                pokemonCondition.getPokemon()!=null||
                pokemonCondition.getAbility()!=null)
        ){
            if (pokemonCondition.getType()!=null && pokemonCondition.getType().equals("")) pokemonCondition.setType(null);
            if (pokemonCondition.getType1()!=null && pokemonCondition.getType1().equals("")) pokemonCondition.setType1(null);
            if (pokemonCondition.getPokemon()!=null && pokemonCondition.getPokemon().equals("")) pokemonCondition.setPokemon(null);
            if (pokemonCondition.getAbility()!=null && pokemonCondition.getAbility().equals("")) pokemonCondition.setAbility(null);
             request.getSession().setAttribute("pokemonCondition",pokemonCondition);
        }else {
            if(request.getSession().getAttribute("pokemonCondition")!=null){
                pokemonCondition= (Pokemon) request.getSession().getAttribute("pokemonCondition");
            }
        }
        Page<Pokemon> pokemonPage =
                pokemonDao.selectPokemonPage(pokemonCondition,
                        pageNum,10);
        request.setAttribute("pokemonPage",pokemonPage);
        return "forward:/web/pokemonPage.jsp";
    }
    @RequestMapping("/detailsOfPokemonPage")
    public String detailsOfPokemonPage(Pokemon pokemonCondition, HttpServletRequest request){
        List<Pokemon> pokemonList=pokemonDao.selectPokemon(pokemonCondition);
        if(pokemonList!=null && pokemonList.size()>0){
            Pokemon pokemon=pokemonList.get(0);
            if(pokemon.getType1()!=null){
                request.setAttribute("typeDefense",
                        typeDao.selectManyTypeDefense(pokemon.getType(),pokemon.getType1()));
            }else{
                request.setAttribute("typeDefense",
                        typeDao.selectTypeDefense(pokemon.getType()));
            }
            List<Map<String, Object>> pokemonMove = pokemonDao.selectMoveByPokemon(pokemon.getPokemon());
            request.setAttribute("pokemonMove",pokemonMove);
            request.setAttribute("title",pokemon.getPokemon());
            request.setAttribute("detailOfPokemon",pokemon);
            return "forward:/web/detailOfPokemon.jsp";
        }else {
            return "forward:/homepage";
        }
    }
}
